﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LED_Editor.Model;
using ComponentFactory.Krypton.Toolkit;

namespace LED_Editor
{
    public partial class ControllerProgrammPreviewForm : KryptonForm
    {

        public static void Preview(Controller controller)
        {
            ControllerProgrammPreviewForm form = new ControllerProgrammPreviewForm();
            form.Controller = controller;
            form.ShowDialog(); 
        }

        public ControllerProgrammPreviewForm()
        {
            InitializeComponent();
        }

        public Controller Controller
        {
            get
            {
                return ledPreviewComponent1.Controller;
            }
            set
            {
                ledPreviewComponent1.Controller = value;
                foreach (FrameStruct item in value.Frames)
                {
                    GradientPanel panel = new GradientPanel();
                    panel.Height = this.flowLayoutPanel1.Height - 24;
                    panel.StartColor = item.StartColor;
                    panel.EndColor = item.EndColor;
                    panel.BorderStyle = BorderStyle.FixedSingle; 
                    flowLayoutPanel1.Controls.Add(panel);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ledPreviewComponent1.Enabled)
            {
                Stop();
            }
            else
            {
                Start();
                //StringBuilder sb = new StringBuilder();
                //foreach (var item in Controller.Frames     )
                //{
                   
                //    sb.AppendLine(item.ToString());
                //}
                //MessageBox.Show(sb.ToString());
            }
        }
        private void Start()
        {
            button1.Text = "Стоп";
            ledPreviewComponent1.Start(); 
        }


        private void Stop()
        {
            button1.Text = "Старт";
            ledPreviewComponent1.Stop(); 
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            ledPreviewComponent1.Ratio  = trackBar1.Value;
        }

        private void ledPreviewComponent1_LedPreviewControllingChanged(object sender, LED_Editor.Controls.LedPreviewControllingEventArgs e)
        {
            panel1.BackColor = e.EffectColor;
            label1.Text = e.ElapsedMilliseconds +" ms"; 
        }

        private void ledPreviewComponent1_LedPreviewStateChanged(object sender, LED_Editor.Controls.LedPreviewStateEventArgs e)
        {
            switch (e.PreviewControllingState )
            {
                case LED_Editor.Controls.LedPreviewControlling.Start:
                    button1.Text = "Стоп";
                    break;
                case LED_Editor.Controls.LedPreviewControlling.Stop:
                    button1.Text = "Старт";
                    break;
                default:
                    break;
            } 
        }

        private void ControllerProgrammPreviewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ledPreviewComponent1.Stop(); 
        }
    }
}
